package com.yidu.qianfeng.controller;


import com.yidu.qianfeng.common.bean.Vehicle.CarInfo;
import com.yidu.qianfeng.common.bean.client.Consignee;
import com.yidu.qianfeng.common.bean.client.Shipper;
import com.yidu.qianfeng.common.bean.company.Subst;
import com.yidu.qianfeng.common.bean.transport.TrackWaybill;
import com.yidu.qianfeng.common.bean.transport.TrackWrap;
import com.yidu.qianfeng.common.bean.transport.TransportProtocol;
import com.yidu.qianfeng.common.bean.transport.TruckLoading;
import com.yidu.qianfeng.common.bean.waybill.Waybill;
import com.yidu.qianfeng.mapper.ChientMapper;
import com.yidu.qianfeng.service.TrackWaybillService;
import com.yidu.qianfeng.service.TruckServiceInter;
import com.yidu.qianfeng.service.impl.FinanceApi;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller("truckController")
public class TruckController {
    //注入跟踪业务实现
    @Resource(name = "trackWaybillServiceImpl")
    private TrackWaybillService trackWaybillService;
    //注入运输业务实现
    @Resource(name = "truckServiceimpl")
    private TruckServiceInter truckServiceInter;

    //注入财务api
    @Resource(name = "financeApi")
    private FinanceApi financeApi;


    //运输协议页面显示
    @RequestMapping("/addtraprotocol")
    public String addtraprotocol() {
        return "views/addtraprotocol";
    }

   //装车配载页面显示
    @RequestMapping("/addLoadingstowage")
    public String addLoadingstowage() {
        return "views/addLoadingstowage";
    }

    //发车列表页面显示
    @RequestMapping("/depart")
    public String depart() {
        return "views/departlist";
    }

    /**
     * 微信小程序登录
     * @param username 用户名
     * @param password 密码
     * @return 是否登录成功
     */
    @RequestMapping("/logins")
    @ResponseBody
    public  boolean login (String username, String password){
        boolean login = truckServiceInter.login (username,password);
        if (login) {
            return true;
        }
        return false;
    }

    /**
     *微信小程序查询跟踪数据
     * @param waybillConsignNum 托运单号
     * @return 跟踪记录集合
     */
    @RequestMapping("/selectwaybilllist")
    @ResponseBody
    public   List<TrackWaybill> selectwaybilllist (String waybillConsignNum){
        System.out.println(waybillConsignNum);
        List<TrackWaybill> list = truckServiceInter.selectwaybilllist (waybillConsignNum);
        System.out.println(list.toString());
        return list;
    }

    /**
     * 查询运单数据
     *
     * @return Map<String, Object> map对象
     */
    /**
     * 查询运单数据
     * @param page 页码数
     * @param limit 条目数
     * @param waybill 属性联合类
     * @param status 运单状态
     * @return Map<String, Object> map对象
     */
    @RequestMapping("/getAll")
    @ResponseBody
    public Map<String, Object> getAll(int page, int limit, TrackWrap waybill, String status) {
        //查询运单数据
        List<TrackWrap> mol = truckServiceInter.selectwaybill((page - 1) * limit, limit,waybill,status);

        //查询发站库存数据行数
        int count = truckServiceInter.selectwaybillnum(status);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", "0");
        map.put("msg", "");
        map.put("count", count);
        map.put("data", mol);
        return map;

    }


    /**
     * 修改运输协议
     * @param loading 装车类
     * @return 新增数目
     */
    @RequestMapping("/updatecar")
    @ResponseBody
    public int updatecar(TruckLoading loading) {
        //调用服务层方法
        int count = truckServiceInter.updatecar(loading);
        return count;
    }


    /**
     * 新增协议信息
     * @param pro 协议类
     * @param request 请求
     * @return 新增条数
     */
    @RequestMapping("/insertagreement")
    @ResponseBody
    public int insertagreement(TransportProtocol pro,HttpServletRequest request) {
        //新增运输协议
        int count = truckServiceInter.insertprotocol(pro);
        //生成发车跟踪记录
        trackWaybillService.generateTrackTrans(pro,UserController.getLoginEmpInfo(request));

        //生成财务记录
        //支出
        financeApi.financeRecord(0, "运输开支", pro.getTransFreightTotal(), "运输费现付", "运输单号："+pro.getTransNum(), pro.getTransReceivingCom(), pro.getTransStarting(), UserController.getLoginEmpInfo(request));
        return count;
    }

    /**
     * 查询分站名称
     * @return 分站数据集合
     */
    @RequestMapping("/subtype")
    @ResponseBody
    public List<Subst> subType() {
        List<Subst> mol = truckServiceInter.selectsubtype();
        return mol;

    }


    /**
     *清空列表
     * @param ids 前端运单号
     * @return 删除数目
     */
    @RequestMapping("/clearall")
    @ResponseBody
    public int clearall(String ids) {
         //根据，号把前台传的运单号分割成数组
        String[] strs = ids.split(",");
        //调用清空装车列表
        int count =truckServiceInter.updatecarstatus(ids);
        System.out.println(count);
        return  count ;

    }


    /**
     * 添加装车表
     * @param ids 前端传入编号
     * @param request 请求
     * @return 数字
     */
    @ResponseBody
    @RequestMapping("/addtruckloading")
    public int addtruckloading(String ids,HttpServletRequest request) {
        //根据，号把前台传的运单号分割成数组
        String[] strs = ids.split(",");
        //定义一个数字变量
        int count = 0;
        //定义一个字符串变量
        String count1 = null;
        //循环数组长度次
        for (int i = 0; i < strs.length; i++) {
            //运单类实例化
            Waybill waybill = new Waybill();
            //设置前端传值为运单编号
            waybill.setWaybillConsignNum(strs[i]);
            //调用准备装车方法
           count1 = trackWaybillService.generateTrackPreLoadCar( waybill,UserController.getLoginEmpInfo(request));
        }
        //判断准备装车方法是否为空
        if (count1!=null){
            //不为空设置变量为1
            count =1 ;
        }
        return count;
    }
    /**
     *添加模拟装车
     * @param id 运单号
     * @param request
     * @return 数字判断值
     */
    @ResponseBody
    @RequestMapping("/changeStatetwo")
    public int changeStatetwo(String id,HttpServletRequest request) {
        //通过运单号查询是否模拟装车
       int pand = truckServiceInter.selectpand(id);
       //定义一个整数和字符串变量
        int count = 0;
        String count1 = null;
        //判断运单能否准备装车
        if(pand==1) {
            //实例化运单类
            Waybill waybill = new Waybill();
            //设置运输单号
            waybill.setWaybillConsignNum(id);
            //新增准备装车跟踪记录
            count1 = trackWaybillService.generateTrackPreLoadCar( waybill,UserController.getLoginEmpInfo(request));
        }
        //判断装车记录是否成功
        if (count1!=null){
            //整数改为一
            count =1 ;
        }
        return count;
    }


    /**
     *移除装车
     * @param ids 前端运单号
     * @return int包装
     */
    @ResponseBody
    @RequestMapping("/removecar")
    public Integer removecar(String ids) {
        //根据，号把前台传的运单号分割成数组
        String[] strs = ids.split(",");
        //定义一个变量
        Integer count = 0;
        //循环字符串数组
        for (int i = 0; i < strs.length; i++) {
            //实例化运单类
            Waybill waybill = new Waybill();
            //设置运输单号
            waybill.setWaybillConsignNum(strs[i]);
            //删除跟踪记录
           count = trackWaybillService.cascadeDeleteTrack( waybill,2);
        }

        return count;
    }


    /**
     * 重新装车
     * @param ids 运单号
     * @param id 运输号
     * @return
     */
    @ResponseBody
    @RequestMapping("/removecartwo")
    public int removecartwo(String ids,String id) {
        //根据，号把前台传的运单号分割成数组strs,str
        String[] strs = ids.split(",");
        String[] str = id.split(",");

        //定义两个变量
        int count = 0;
        int count1 =0;
        //循环字符串strs数组
        for (int i = 0; i < strs.length; i++) {
            //实例化运单类
            Waybill waybill = new Waybill();
            //设置运输单号
            waybill.setWaybillConsignNum(strs[i]);
            //删除装车的跟踪记录
            count = trackWaybillService. cascadeDeleteTrack( waybill,3);
        }
        //循环字符串str数组
        for (int i = 0; i < str.length; i++) {
            //删除选中装车
            count1 = truckServiceInter.deletecarone(Integer.parseInt(str[i]));
        }
        return count1;
    }

    /**
     * 保存装车
     * @param ids 装车清单
     * @param sum 总件数
     * @param length 总件数
     * @param weight 总重量
     * @param Volume 总体积
     * @param Freight 总运费
     * @param Extractfee 总提车费
     * @param Sendfare  总送车费
     * @param ExpenseTotal 费用合计
     * @param Starting 出发站
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/savecar")
    public int savecar(String ids, String sum, String length, String weight, String Volume, String Freight, String Extractfee, String Sendfare, String ExpenseTotal, String Starting, HttpServletRequest request) {
        //根据，号把前台传的运单号分割成数组
        String[] strs = ids.split(",");
        //定义一个变量
        int count =0;
            //获取当前时间
            String now = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            //把值保存到装车对象装车序号自增长运输号,车号为空
            TruckLoading loading = new TruckLoading(0, null, null, ids,UserController.getLoginEmpInfo(request).getEmpName(), now, Integer.parseInt(length), Starting, Integer.parseInt(sum),
                    Double.parseDouble(weight), Double.parseDouble(Volume), Double.parseDouble(Freight), Double.parseDouble(Extractfee), Double.parseDouble(Sendfare), Double.parseDouble(ExpenseTotal));
            //新增装车跟踪表
            trackWaybillService.generateTrackLoadCar(loading,UserController.getLoginEmpInfo(request));
            //保存装车
             count = truckServiceInter.insertcar(loading);
        return count;
    }

    /**
     * 保存数据到装车清单表
     * @param ids 运单号
     * @return状态
     */
    @ResponseBody
    @RequestMapping("/relation")
    public int relation(String ids) {
        //根据，号把前台传的运单号分割成数组
        String[] strs = ids.split(",");
        //查询装车序号
       String  trucknum = truckServiceInter.selecttrucknum(ids);
        //定义一个变量
        int count = 0;
      //调用保存的方法
        for (int i = 0; i < strs.length; i++) {
             count = truckServiceInter.relation(strs[i], trucknum);
        }
        return count;
    }

    /**
     * 删除装车清单表数据
     * @param id 装车序号
     * @return状态
     */
    @ResponseBody
    @RequestMapping("/deleterelation")
    public int deleterelation(String id) {
        //根据，号把前台传的运单号分割成数组
        String[] strs = id.split(",");
        //定义一个变量
        int count = 0;
        //循环字符串数组
        for (int i = 0; i < strs.length; i++) {
            //把当前循环的传到biz进行修改 用整数变量s接收
            count = truckServiceInter.deleterelation(Integer.parseInt(strs[i]));
        }
        return count;
    }

    /**
     * 查询装车数据
     *
     * @return Map<String, Object> map对象
     */
    @RequestMapping("/truckloading")
    @ResponseBody
    public Map<String, Object> truckloading(int page, int limit) {
        //查询分页装车数据
        List<TruckLoading> mol = truckServiceInter.selecttruckloading((page - 1) * limit, limit);
        //查询装车表数据行数
        int count = truckServiceInter.selectcarnumtwo();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", "0");
        map.put("msg", "");
        map.put("count", count);
        map.put("data", mol);
        return map;

    }


    /**
     *查询发车列表数据
     * @param page 页码数
     * @param limit 条目数
     * @param wrap 属性联合表
     * @return Map<String, Object> map对象
     */
    @RequestMapping("/depatlist")
    @ResponseBody
    public Map<String, Object> depatlist(int page, int limit,TrackWrap wrap) {
        //分页查询发车列表数据
        List<TrackWrap> mol = truckServiceInter.depatlist((page - 1) * limit, limit,wrap);
        //查询发车列表数据行数
        int count = truckServiceInter.depatlistnum();
        //map对象保存发车数据
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", "0");
        map.put("msg", "");
        map.put("count", count);
        map.put("data", mol);
        return map;
    }


    /**
     * 查询发车列表数据
     *
     * @return Map<String, Object> map对象
     */
    @RequestMapping("/selectcarlist")
    @ResponseBody
    public List<TruckLoading> selectcarlist(String id) {
        List<TruckLoading> mol = truckServiceInter.selectcarlist(id);
        return mol;
    }

    /**
     * 添加司机
     * @param info 司机类
     * @return 新增条数
     */
    @RequestMapping("/insertdriver")
    @ResponseBody
    public int insertdriver(CarInfo info) {
        //定义两个变量
        int count=0;
        int count1=0;
        //查询司机是否存在
        count = truckServiceInter.selectdriver(info);
        //判断是否能新增
        if(count==0) {
            //通过司机表分站名称查询分站信息
            Subst subst= truckServiceInter.selectsubtypeone(info);
            //设置司机分站名称为分站编号
            info.setCarPartofSubs(subst.getSubstId());
            //保存司机
            count1 = truckServiceInter.insertdriver(info);
        }
        return count1;
    }



            /**
             * 删除协议
             * @param ids  运输号
             * @param id 装车序号
             * @param carloadlist 装车清单号
             * @param request
             * @return 整数
             */
            @RequestMapping("/deletecarlist")
            @ResponseBody
            public int deletecarlist(String ids,String id,String carloadlist,HttpServletRequest request) {
                //运单号strs
                String[] strs = ids.split(",");
                //运输装车序号str
                String[] str = id.split(",");
                //装车清单list
                String[] list = carloadlist.split(",");
                //定义四个整数变量
                int count = 0;
                int count1 = 0;
                int count2 = 0;
                int count3 = 0;
                //循环字符串list数组
                for (int i = 0; i < list.length; i++) {
                    //判断能否删除协议
                    count=truckServiceInter.selectdelnum(list[i]);
                    if (count==4){
                        //实例化运单类
                        Waybill waybill = new Waybill();
                        waybill.setWaybillConsignNum(list[i]);
                        //删除发运跟踪记录
                        count1=trackWaybillService.cascadeDeleteTrack( waybill,4);
                    }
                }
                //循环字符串strs数组
                for (int i = 0; i < strs.length; i++) {
                    if (count==4){
                        //删除发车列表
                        count2 = truckServiceInter.deletecarlist(strs[i]);
                        //修改装车表
                        count3= truckServiceInter.updatecarone(Integer.parseInt(str[i]));
                    }
                }
                 //返回整数之和
                return count1+count2+count3;
            }

    /**
     * 修改运输协议
     * @param t 运输协议类
     * @return 修改数目
     */
    @RequestMapping("/updatecarlist")
    @ResponseBody
    public int updatecarlist(TransportProtocol t) {
       //修改协议表
        int count = truckServiceInter.updateprotocol(t);
        return count;
    }


    /**
     * 模糊查询司机
     * @param carDriver 司机姓名
     * @param startingSubst 出发站
     * @return 联合属性类集合
     */
    @RequestMapping("/selectdriverinfo")
    @ResponseBody
    public List<TrackWrap> selectdriverinfo(String carDriver,String startingSubst) {
        //查询司机
        List<TrackWrap> trackWraps = truckServiceInter.selectdriverinfo(carDriver,startingSubst);

        return trackWraps;
    }

    /**
     * 模糊查询收货人
     * @param consiCom 收货人姓名
     * @return 收货人类集合
     */
    @RequestMapping("/selectconsinfo")
    @ResponseBody
    public List<Consignee> selectconsinfo(String consiCom) {
        List<Consignee> consignees = truckServiceInter.selectconsinfo(consiCom);

        return consignees;
    }
}

